/** @format */

import { findPass } from '../utils/findPass'
import { formatDBQuery, formatUpdateOperator } from '../utils/formatWx'

export class UniBaseDao {
  private db: any
  constructor(context: any, private dbName: string) {
    // @ts-ignore
    this.db = uniCloud.database() // 使用uniCloud的数据库实例 uniCloud.database()
    this.dbName = dbName
  }

  /**
   * 查询一条数据
   * @param query 查询条件
   * @param projection 投影/筛选条件
   */
  async findOne(query: Record<string, any> = {}, projection: Record<string, any> = {}) {
    let dbCmd = this.db.collection(this.dbName)
    
    // 处理查询条件
    if (Object.keys(query).length > 0) {
      dbCmd = dbCmd.where(query)
    }

    // 处理字段筛选
    if (projection.projection) {
      dbCmd = dbCmd.field(projection.projection)
    }

    // 处理排序
    if (projection.sort) {
      for (const [field, order] of Object.entries(projection.sort)) {
        dbCmd = dbCmd.orderBy(field, order === 1 ? 'asc' : 'desc')
      }
    }

    // 处理分页
    if (projection.skip) {
      dbCmd = dbCmd.skip(projection.skip)
    }

    const result = await dbCmd.limit(1).get()
    return result.data[0]
  }

  /**
   * 查询多条数据
   * @param query 查询条件
   * @param projection 投影/筛选条件
   */
  async find(query: Record<string, any> = {}, projection: Record<string, any> = {}) {
    let dbCmd = this.db.collection(this.dbName)
    
    // 处理查询条件
    if (Object.keys(query).length > 0) {
      dbCmd = dbCmd.where(query)
    }

    // 处理字段筛选
    if (projection.projection) {
      dbCmd = dbCmd.field(projection.projection)
    }

    // 处理排序
    if (projection.sort) {
      for (const [field, order] of Object.entries(projection.sort)) {
        dbCmd = dbCmd.orderBy(field, order === 1 ? 'asc' : 'desc')
      }
    }

    // 处理分页
    if (projection.skip) {
      dbCmd = dbCmd.skip(projection.skip)
    }
    if (projection.limit) {
      dbCmd = dbCmd.limit(projection.limit)
    }

    const result = await dbCmd.get()
    return result.data
  }

  /**
   * 插入单条数据
   * @param document 要插入的文档
   */
  async insertOne(document: Record<string, any>) {
    const res = await this.db.collection(this.dbName).add(document)
    return res.id // UniCloud返回的是id而不是_id
  }

  /**
   * 插入多条数据
   * @param documents 要插入的文档数组
   */
  async insertMany(documents: Record<string, any>[]) {
    const res = await this.db.collection(this.dbName).add(documents)
    return res.ids // UniCloud批量插入返回ids数组
  }

  /**
   * 更新数据
   * @param query 查询条件
   * @param updateData 更新数据
   */
  async update(query: Record<string, any>, updateData: Record<string, any>) {
    const res = await this.db.collection(this.dbName)
      .where(query)
      .update(updateData)
    
    return res.updated // 返回更新的记录数
  }

  /**
   * 删除数据
   * @param filter 删除条件
   */
  async delete(filter: Record<string, any>) {
    const res = await this.db.collection(this.dbName)
      .where(filter)
      .remove()
    
    return res.deleted // 返回删除的记录数
  }

  /**
   * 获取条目数
   * @param query 查询条件
   */
  async count(query: Record<string, any> = {}) {
    const res = await this.db.collection(this.dbName)
      .where(query)
      .count()
    
    return res.total
  }

  /**
   * 聚合查询
   * @param pipeline 聚合管道
   */
  async aggregate(pipeline: any[]) {
    const res = await this.db.collection(this.dbName)
      .aggregate(pipeline)
      .end()
    
    return res.data
  }
}